import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/dashboard.vue'
import Error from './views/404.vue'
import login from './views/login'
import store from './store.js'
let auth = localStorage.getItem('auth');
if(auth === 'undefined') {
    auth = '{}'
}
Vue.use(Router);

const router =  new Router({
    routes: [
        {
            path: '/dashboard',
            meta:{
                needAuth:true
            },
            name: 'home',
            component: Home
        },
        {
            path:'/',
            meta:{
                needAuth:true
            },
            components:{
                login:login
            }
        },
        {
            path: '/sale-approval',
            component: () => import('./views/sale/approveQuest.vue')
        },
        {
            path: '/info-collect',
            component: () => import('./views/sale/infoCollect.vue')
        },
        {
            path: '/my-sale-approval',
            component: () => import('./views/sale/myApproval.vue')
        },
        {
            path: '/approve-sale-approval',
            component: () => import('./views/sale/approve.vue')
        },
        {
            path: '/sale-follow',
            component: () => import('./views/sale/saleContract.vue')
        },
        {
            path: '/my-contract',
            component: () => import('./views/sale/mySaleContract.vue')
        },
        {
            path:'/unit-quotation',
            component:()=> import('./views/sale/unitQuotation.vue')
        },
        {
            path:'/part-quotation',
            component:()=> import('./views/sale/partQuotation.vue')
        },
        {
            path:'/sales-order',
            component:()=> import('./views/sale/salesOrder')
        },


        // purchase
        {
            path:'/vendor',
            component:()=> import('./views/purchase/vendor.vue')
        },
        {
            path:'/purchase-request',
            component:() => import('./views/purchase/purchaseRequest')
        },
        {
            path:'/purchase-approve',
            component:() => import('./views/purchase/purchaseApprove')
        },
        {
            path:'/request-purchase',
            component:() => import('./views/purchase/requestPurchase')
        },
        {
            path:'/purchase-order',
            component:() => import('./views/purchase/purchaseOrder')
        },

        // export

        {
            path:'/export',
            component:() => import('./views/export/export.vue')
        },
        // import
        {
            path:'/import',
            component:() => import('./views/export/import.vue')
        },

        // storck in
        {
            path:'/stock-in',
            component:() => import('./views/export/stockIn.vue')
        },
        {
            path:'/stock-in-list',
            component:() => import('./views/export/stockInList.vue')
        },

        // storage
        {
            path:'/storage',
            component: ()=> import('./views/export/storage.vue')
        },
        // stock out
        {
            path:'/stock-out',
            component: () => import('./views/export/stockOut.vue')
        },
        //sparepart database
        {
            path:'/part-list',
            component: () => import('./views/part/partlist')
        },
        //unit\
        {
            path:'/unit-type',
            component: () => import('./views/part/unitType')
        },
        {
            path:'/property',
            component:() => import('./views/part/property')
        },
        {
            path:'/unit-list',
            component: () => import('./views/part/unitlist')
        },
        {
            path:'/unit-field',
            component: () => import('./views/part/unitOptionType')
        },
        {
            path:'/unit-option',
            component:() => import('./views/part/unitOptionList')
        },
        //service
        {
            path:'/quest',
            component:() => import('./views/service/quest')
        },
        {
            path:'/work-order',
            component:() => import('./views/service/workingOrder')
        },
        {
            path:'/service-stock-out',
            component:() => import('./views/service/stockOut')
        },
        {
            path:'/close-order',
            component:() => import('./views/service/closeOrder')
        },
        {
            path:'/service-stock-in',
            component:() => import('./views/service/stockIn')
        },
        {
            path:'/service-report',
            component:() => import('./views/service/report')
        },
        {
            path:'/service-report1',
            component: () => import('./views/service/Order')
        },
        {
           path:'/service-equipment',
           component: () => import('./views/service/equipment')
        },
        {
            path:'/service-project',
            component:() => import('./views/service/project')
        },
        {
            path:'/point-code',
            component:() => import('./views/service/pointCode')
        },
        {
            path:'/cost',
            component: ()=> import('./views/service/cost')
        },
        {
            path:'/invoice',
            component:() => import('./views/service/invoice')
        },
        {
            path:'/inventory',
            component:() => import('./views/service/inventory')
        },
        //information
        {
            path:'/employee',
            component:() => import('./views/information/employee')
        },
        {
            path:'/customer',
            component: () => import('./views/information/customer')
        },
        {
            path:'/sys-customer',
            meta:{
                needAuth:true
            },
            component:() => import('./views/information/systemCustomer')
        },
        {
            path:'/warehouse',
            component: () => import('./views/information/warehouse')
        },
        //auth
        {
            path:'/user-manage',
            component:() => import('./views/auth/userManage')
        },
        {
            path:'/role-manage',
            component:() => import('./views/auth/roleManage')
        },
        {
            path:'/auth-manage',
            component:() => import('./views/auth/AuthManage')
        },

        //customer
        {
            path:'/customer-request',
            component:() => import('./views/customerQuest/purchaseRequest')
        },
        {
            path:'/customer-approve',
            component:() => import('./views/customerQuest/purchaseApprove')
        },
        //寄售合同 shanqi
        {
            path:'/shanqi/consignment',
            component:() => import('./views/consignment/consignment')
        },
        {
            path:'/shanqi/export',
            component:() => import('./views/shanxi/export')
        },
        // import
        {
            path:'/shanqi/import',
            component:() => import('./views/shanxi/import')
        },
        {
            path:'/shanqi/stockin',
            component:()=> import('./views/shanxi/stockIn')
        },
        {
            path:'/shanqi/stockout',
            component:()=> import('./views/shanxi/stockOut')
        },
        {
            path:'/shanqi/inventory',
            component:()=> import('./views/shanxi/inventory')
        },
        {
            path:'/unAuth',
            meta:{
                needAuth:true
            },
            component:() => import('./views/unAuth')
        },

        {
            path: '*',
            component: Error
        }
    ]
});
router.beforeEach((to,from,next)=>{
    let menu = store.state.auth && store.state.auth.menu && store.state.auth.menu.length ?
        store.state.auth.menu :
        JSON.parse(auth) && JSON.parse(auth).menu ? JSON.parse(auth).menu : [];
    if (menu.length || to.meta.needAuth){
        if(to.meta.needAuth || menu.some(auth=>auth.url === to.path)) {
            next();
        } else {
            next('/unAuth')
        }
    } else {
        next('/')
    }

});
export default router;
